package com.swas.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.swas.entity.PatrolPlan;
import com.swas.service.PatrolPlanService;
import com.swas.util.ResultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/patrolplan")
public class PatrolPlanController {
    @Autowired
    private PatrolPlanService patrolPlanService;
    @GetMapping("/findAll")
    public ResultVO findAll(@RequestParam(defaultValue = "1") Integer pageNum,
                            @RequestParam(defaultValue = "5") Integer pageSize,
                            String planno,
                            Integer pattype,
                            Integer patcycle,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date startdate,
                            @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") Date enddate){
        if (enddate == null) {
            enddate = new Date();
        }
        PageHelper.startPage(pageNum,pageSize);
        List<PatrolPlan> patrolPlanList = patrolPlanService.findAll(pageNum,pageSize,planno,pattype,patcycle,startdate,enddate);
        PageInfo<PatrolPlan> patrolPlanPageInfo = new PageInfo<>(patrolPlanList);
        return ResultVO.success("ok",patrolPlanPageInfo);
    }
    @GetMapping(value = "/{id}")
    public ResultVO findOne(Integer id){
        PatrolPlan p = patrolPlanService.fiandOne(id);
        if (p!=null){
            return ResultVO.success("ok",p);
        }
        return ResultVO.fail("on ok");
    }
    @PostMapping("/addPatrolPlan")
    public ResultVO addPatrolPlan(@RequestBody PatrolPlan patrolPlan){
        boolean b = patrolPlanService.addPatrolPlan(patrolPlan);
        if (b){
            return ResultVO.success("添加成功");
        }
        return ResultVO.fail("添加失败");
    }
}
